<template>
    <!-- cont 总是按高度或宽度铺满屏幕（cover） -->
    <!--  页面缩放  -->
    <!--<div ref="cont" class="fit-screen-cont" >
        &lt;!&ndash; content 通过 scale(实际尺寸/设计尺寸) 来缩放实现等比适配  &ndash;&gt;
        <div class="fit-content" :style="getFitContentStyle()" >
            <slot ref="content"  class="fit-content"></slot>
        </div>
    </div>-->
    <div>
        <el-row :gutter="20">
            <el-col :span="6">
                <el-card shadow="hover" class="mgb20" style="height:200px;">
                    <div class="user-info">
                        <img :src="user.avatar" class="user-avator" alt @click='changeAvatar'/>
                        <div class="user-info-cont">
                            <div class="user-info-name">{{user.name}}</div>
                            <div>
                                <div v-for="role in user.roles" style='display:inline-block; padding-left: 5px; padding-top: 3px;'>
                                    <el-tag>{{role.nameZh}}</el-tag>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div>
                        <table>
                            <tr>
                                <td style='width: 250px;'>上次登录时间：</td>
                                <td style='width: 150px;'>
                                    <el-tag>{{ user.lastLoginInfo.lastLoginTime }}</el-tag>
                                </td>
                            </tr>
                            <tr>
                                <td>上次登录IP：</td>
                                <td>
                                    <el-tag>{{ user.lastLoginInfo.lastLoginIp }}</el-tag>
                                </td>
                            </tr>
                        </table>
                    </div>
                </el-card>
                <el-card shadow="hover" style="height:260px;">
                    <div slot="header" class="clearfix">
                        <span>语言详情</span>
                    </div>
                    Java
                    <el-progress :percentage="80.5" color="#42b983"></el-progress>
                    JavaScript
                    <el-progress :percentage="64.3" color="#f1e05a"></el-progress>
                    Vue
                    <el-progress :percentage="44.7"></el-progress>
                    Python
                    <el-progress :percentage="40.2" color="#f56c6c"></el-progress>
                    Go
                    <el-progress :percentage="18.9" color="purple"></el-progress>
                </el-card>
            </el-col>
            <el-col :span="18">
                <el-row :gutter="20" class="mgb20">
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}" class='dataHeight'>
                            <div class="grid-content grid-con-1">
                                <i class="el-icon-user grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ statistics.visitNum}}</div>
                                    <div>用户访问量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}" class='dataHeight'>
                            <div class="grid-content grid-con-2">
                                <i class="el-icon-user-solid grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ statistics.visitorNum}}</div>
                                    <div>用户数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                    <el-col :span="8">
                        <el-card shadow="hover" :body-style="{padding: '0px'}" class='dataHeight'>
                            <div class="grid-content grid-con-3">
                                <i class="el-icon-lx-notice grid-con-icon"></i>
                                <div class="grid-cont-right">
                                    <div class="grid-num">{{ statistics.msgNum }}</div>
                                    <div>消息数量</div>
                                </div>
                            </div>
                        </el-card>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="mgb20">
                    <el-col :span='16'>
                        <el-card shadow="hover" class='todoItemHeight'>
                            <div slot="header" class="clearfix">
                                <span>待办事项</span>
                                <el-button style="float: right; padding: 3px 0" type="text" @click='addTodo'>添加</el-button>
                            </div>
                            <el-table :show-header="true" height='300px;' :data="todoList" style="width:100%;height: 300px; overflow: auto; flex: auto;"
                                      v-el-table-infinite-scroll="load" infinite-scroll-disabled="disabled">
                                <el-table-column width="60">
                                    <template slot-scope="scope">
                                        <span style='color: #3b6be3'>{{ scope.row.index }}</span>
                                    </template>
                                </el-table-column>
                                <el-table-column label="事项名称">
                                    <template slot-scope="scope">
                                        <div
                                            class="todo-item"
                                            :class="{'todo-item-del': scope.row.finishStatus}"
                                        >{{scope.row.itemName}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="计划完成时间" width="120">
                                    <template slot-scope='scope'>
                                        <div>{{ scope.row.planFinishTime }}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column width="80" label="操作">
                                    <template slot-scope='scope'>
                                        <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                                            <span style='cursor: pointer; color: #3b6be3' @click='editItem(scope.row)'><i class="el-icon-edit"></i></span>
                                        </el-tooltip>
                                        <el-tooltip class="item" effect="dark" content="确认完成" placement="top">
                                            <span style='padding-left: 10px; cursor: pointer; color: blue' @click='finishItem(scope.row.id)'><i class="el-icon-check"></i></span>
                                        </el-tooltip>
                                        <el-tooltip class="item" effect="dark" content="删除" placement="top">
                                            <span style='padding-left: 10px; cursor: pointer; color: red' @click='delItem(scope.row.id)'><i class="el-icon-delete"></i></span>
                                        </el-tooltip>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-card>
                    </el-col>

                    <el-col :span="8">
                        <el-card shadow="hover" class='todoItemHeight'>
                            <div slot="header" class="clearfix">
                                <span>已完成事项</span>
                                <!--<el-button style="float: right; padding: 3px 0" type="text" @click='addTodo'>添加</el-button>-->
                            </div>
                            <el-table :show-header="true" height='300px;' :data="finishedList" style="width:100%;height: 300px; overflow: auto; flex: auto;"
                                      v-el-table-infinite-scroll="load" infinite-scroll-disabled="disabled">
                                <el-table-column width="30">
                                    <template slot-scope="scope">
                                        <div><span class='el-icon-success' style='color: #5daf34'></span></div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="事项名称">
                                    <template slot-scope="scope">
                                        <div class="todo-item">{{scope.row.itemName}}</div>
                                    </template>
                                </el-table-column>
                                <el-table-column label="实际完成时间" width="100">
                                    <template slot-scope='scope'>
                                        <div>{{ scope.row.realFinishTime }}</div>
                                    </template>
                                </el-table-column>
                                <!--<el-table-column width="60">
                                    <template>
                                        <i class="el-icon-edit"></i>
                                        <i class="el-icon-delete"></i>
                                    </template>
                                </el-table-column>-->
                            </el-table>
                        </el-card>
                    </el-col>
                </el-row>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="24">
                <el-card shadow="hover" style='height: 300px;'>
                    <schart ref="bar" class="schart" canvasId="bar" :options="options"></schart>
                </el-card>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import Schart from 'vue-schart';
import bus from '../components/common/bus';
import { Message } from 'element-ui';

import { deleteToDoItem, getOverView, getRecentVisitData, getToDoList, updateToDoItem } from '@/api/DashboardApi';

export default {
    name: 'dashboard',
    data() {
        return {

            width:1920,
            height:1080,
            currentRatio:1,

            confirmId: '',

            statistics: {
                visitNum: 0,
                visitorNum: 0,
                msgNum: 0,
            },

            name: localStorage.getItem('ms_username'),
            todoList: [],
            finishedList: [],
            recentDays: 14,
            options: {
                type: 'bar',
                title: {
                    text: '最近14天访问数据'
                },
                xRorate: 0,
                labels: [],
                datasets: [
                    {
                        label: '访问次数',
                        data: []
                    },
                    {
                        label: '访问用户数',
                        data: []
                    },
                ]
            },
        };
    },
    components: {
        Schart
    },
    computed: {
        user() {
            return JSON.parse(localStorage.getItem('user'));
        },
    },
    mounted() {
        /*页面缩放*/
        /*window.addEventListener('resize',()=>{
            this.updateScaleRatio();
        })
        this.updateScaleRatio();*/

        this.initData();
        this.initToDoList();
        this.initFinishedList();
        this.initRecentVisitDataChart();
    },
    created() {
        // 使用bus接收事件进行组件间的通信
        bus.$on('showAddDoItem', args => {
            if(!args.show) {
                this.initToDoList();
            }
        });
    },
    methods: {
        /*页面缩放*/
        /*updateScaleRatio(){
            var rect = this.$refs.cont && this.$refs.cont.getBoundingClientRect();
            var ratio = rect.width / rect.height;
            var innerRatio = this.width / this.height;
            if(ratio < innerRatio){
                //Scale width
                var scaleRatio = rect.width/ this.width ;
                this.currentRatio = scaleRatio;
            }
            else{
                scaleRatio = rect.height / this.height ;
                this.currentRatio = scaleRatio;
            }
        },
        getFitContentStyle(){
            return {
                width:this.width + 'px',
                height:this.height + 'px',
                transform:`scale(${this.currentRatio})`
            }
        },*/


        initRecentVisitDataChart() {
            getRecentVisitData({recentDays: this.recentDays}).then(resp => {
                if (resp && resp.status === 200) {
                    this.options.labels = resp.data.dateList;
                    this.options.datasets = resp.data.datasets;
                }
            })
        },

        finishItem(id) {
            updateToDoItem({id: id, finishStatus: 1}).then(resp => {
                if (resp && resp.status === 200) {
                    this.initToDoList();
                    this.initFinishedList();
                }
            })
        },
        initToDoList() {
            const obj = {};
            obj.finishStatus = 0;
            const sortFields = [];
            const sortField = {};
            sortField.propName = 'planFinishTime';
            sortField.order = 'ASC';
            sortFields.push(sortField);
            obj.sortFields = sortFields;
            getToDoList(obj).then(resp => {
                if (resp && resp.status === 200) {
                    this.todoList = resp.data;
                }
            });
        },
        initFinishedList() {
            const obj = {};
            obj.finishStatus = 1;
            const sortFields = [];
            const sortField = {};
            sortField.propName = 'realFinishTime';
            sortField.order = 'DESC';
            sortFields.push(sortField);
            obj.sortFields = sortFields;
            getToDoList(obj).then(resp => {
                if (resp && resp.status === 200) {
                    this.finishedList = resp.data;
                }
            });
        },

        load() {
            alert("加载下一页");
        },
        editItem(row) {
            const obj = {};
            obj.show = true;
            obj.operate = "编辑";
            obj.row = row;
            bus.$emit('showAddDoItem', obj);
        },
        delItem(id) {
            deleteToDoItem({id: "" + id}).then(resp => {
                if (resp && resp.status === 200) {
                    this.initToDoList();
                    Message.success("删除待办事项成功！");
                }
            });
        },
        initData() {
            getOverView().then(resp => {
                if (resp && resp.status === 200) {
                    this.statistics = resp.data;
                }
            });
        },
        // changeDate() {
        //     const now = new Date().getTime();
        //     this.data.forEach((item, index) => {
        //         const date = new Date(now - (6 - index) * 86400000);
        //         item.name = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
        //     });
        // },
        // handleListener() {
        //     bus.$on('collapse', this.handleBus);
        //     // 调用renderChart方法对图表进行重新渲染
        //     window.addEventListener('resize', this.renderChart);
        // },
        // handleBus(msg) {
        //     setTimeout(() => {
        //         this.renderChart();
        //     }, 200);
        // },
        // renderChart() {
        //     this.$refs.bar.renderChart();
        //     this.$refs.line.renderChart();
        // },

        changeAvatar() {
            // Message.success("修改头像");
            console.log("点击修改头像...")
        },

        addTodo() {
            const obj = {};
            obj.show = true;
            obj.operate = "新增";
            obj.row = null;
            bus.$emit('showAddDoItem', obj);
        },
    }
};
</script>


<style scoped>

.todoItemHeight{
    height: 370px;
}

.dataHeight{
    height: 90px;
}

.grid-content {
    display: flex;
    align-items: center;
    height: 100px;
}

.grid-cont-right {
    flex: 1;
    text-align: center;
    font-size: 14px;
    color: #999;
}

.grid-num {
    font-size: 30px;
    font-weight: bold;
}

.grid-con-icon {
    font-size: 50px;
    width: 100px;
    height: 100px;
    text-align: center;
    line-height: 100px;
    color: #fff;
}

.grid-con-1 .grid-con-icon {
    background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
    background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
    color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
    background: rgb(0, 250, 220);
}

.grid-con-3 .grid-num {
    color: rgb(42, 50, 30);
}

.grid-con-4 .grid-con-icon {
    background: rgb(0, 188, 30);
}

.grid-con-4 .grid-num {
    color: rgb(0, 210, 67);
}

.grid-con-5 .grid-con-icon {
    background: rgb(0, 133, 80);
}

.grid-con-5 .grid-num {
    color: rgb(0, 86, 120);
}

.grid-con-6 .grid-con-icon {
    background: rgb(0, 210, 50);
}

.grid-con-6 .grid-num {
    color: rgb(0, 110, 87);
}

.user-info {
    display: flex;
    align-items: center;
    padding-bottom: 2px;
    border-bottom: 2px solid #9bb3e3;
    margin-bottom: 2px;
}

.user-avator {
    width: 110px;
    height: 110px;
    border-radius: 50%;
}

.user-info-cont {
    padding-left: 50px;
    flex: 1;
    font-size: 14px;
    color: #999;
}

.user-info-cont div:first-child {
    font-size: 30px;
    color: #222;
}

.user-info-list {
    font-size: 14px;
    color: #999;
    line-height: 25px;
}

.user-info-list span {
    margin-left: 70px;
}

.mgb20 {
    margin-bottom: 20px;
}

.todo-item {
    font-size: 14px;
}

.todo-item-del {
    text-decoration: line-through;
    color: #999;
}

.schart {
    width: 100%;
    height: 300px;
}

/*页面缩放*/
/*.fit-screen-cont{
    display:flex;
    justify-content: center;
    align-items: center;
    width:100%;
    height:100%;
    flex:1;
    overflow:hidden;
}
.fit-content{
    flex:1;
    transform-origin: center;
    overflow: hidden;
    position: absolute;
}*/

</style>
